package liujinliang.natappvip.cc.ccall.view;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

import liujinliang.natappvip.cc.ccall.R;
import liujinliang.natappvip.cc.ccall.model.Contact;

public class ContactAdapter extends RecyclerView.Adapter<ContactAdapter.ViewHolder> {

    private List<Contact> contactList;

    public ContactAdapter() {
    }

    public ContactAdapter(List<Contact> contactList) {
        this.contactList = contactList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        // 初始化 View
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.address_layout, viewGroup, false);
        // 初始化 ViewHolder
        final ViewHolder holder = new ViewHolder(view);

        // 在此处添加点击事件
        holder.contactView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Contact contact = contactList.get(position);
                Toast.makeText(v.getContext(), "you clicked view " + contact.getContactName(), Toast.LENGTH_SHORT).show();
            }
        });
        holder.contactImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int position = holder.getAdapterPosition();
                Contact contact = contactList.get(position);
                Toast.makeText(v.getContext(), "you clicked image " + contact.getContactName(), Toast.LENGTH_SHORT).show();
            }
        });

        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder viewHolder, int i) {
        Contact contact = contactList.get(i);
        viewHolder.contactImage.setImageResource(contact.getImageId());
        viewHolder.contactName.setText(contact.getContactName());
        viewHolder.contactPhone.setText(contact.getContactPhone());
    }

    @Override
    public int getItemCount() {
        return contactList.size();
    }


    /**
     * 拿到每一个Item(View)，获取元数据
     */
    static class ViewHolder extends RecyclerView.ViewHolder {

        /**
         * 用来拿到当前的View，可以用来获取点击事件
         */
        View contactView;

        /**
         * 联系人头像，姓名，手机号
         */
        ImageView contactImage;
        TextView contactName;
        TextView contactPhone;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            contactView = itemView;
            contactImage = itemView.findViewById(R.id.contact_image);
            contactName = itemView.findViewById(R.id.contack_name);
            contactPhone = itemView.findViewById(R.id.contack_phone);
        }
    }

}
